﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data.SqlClient;
using System.Data;

namespace DAO
{
    public class TaiKhoanDiaDiemDAO
    {
        #region lay tat ca
        public static List<TaiKhoanDiaDiemDTO> getAll()
        {
            SqlConnection connect = null;
            List<TaiKhoanDiaDiemDTO> list = new List<TaiKhoanDiaDiemDTO>();
            try
            {
                connect = DataProvider.Connect();
                string strCommand = "select * from taikhoan_diadiem";
                SqlCommand command = new SqlCommand(strCommand, connect);
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    TaiKhoanDiaDiemDTO d = new TaiKhoanDiaDiemDTO();
                    d.Id = reader.GetInt32(0);
                    if(!reader.IsDBNull(1))
                        d.TaiKhoanId = reader.GetInt32(1);
                    if (!reader.IsDBNull(2))
                        d.DiaDiemId = reader.GetInt32(2);
                    if (!reader.IsDBNull(3))
                        d.Share = reader.GetInt32(3);
                    list.Add(d);
                }
            }
            catch (Exception ex)
            {
                list = null;
            }
            finally
            {
                if (connect != null && connect.State == System.Data.ConnectionState.Open)
                    connect.Close();
            }
            return list;
        }
        #endregion

        #region them
        public static bool insertAccount_DiaDiem(TaiKhoanDiaDiemDTO td)
        {
            bool result = true;
            SqlConnection connect = null;
            try
            {
                connect = DataProvider.Connect();
                string strCommand = "insert into taikhoan_diadiem(taikhoanid, diadiemid, share)"
                + " values(@taikhoanid, @diadiemid, @share)";
                SqlCommand command = new SqlCommand(strCommand, connect);

                SqlParameter param;
                param = new SqlParameter("@taikhoanid", SqlDbType.Int);
                param.Value = td.TaiKhoanId;
                command.Parameters.Add(param);

                param = new SqlParameter("@diadiemid", SqlDbType.Int);
                param.Value = td.DiaDiemId;
                command.Parameters.Add(param);

                param = new SqlParameter("@share", SqlDbType.Int);
                param.Value = td.Share;
                command.Parameters.Add(param);

                int n = command.ExecuteNonQuery();
                if (n == 0)
                {
                    result = false;
                }
            }
            catch (Exception ex)
            {
                result = false;
            }
            finally
            {
                if (connect != null && connect.State == System.Data.ConnectionState.Open)
                    connect.Close();
            }
            return result;
        }
        #endregion
    }
}
